#博客编辑页测试
from selenium.webdriver.common.by import By
from common.Utils import BlogDriver
import time

class BlogEdit:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_edit.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

#页面元素检查--标题输入框、内容输入区、点击发布按钮
    def EditHtmlTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#title")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.content-edit")
        self.driver.find_element(By.CSS_SELECTOR, "#submit")

#登录状态下--正常发布博客
    def EditSucTestByLogin(self):
        self.driver.find_element(By.CSS_SELECTOR, "#title").send_keys("这是另一篇博客标题") #找标题输入框输入标题
        #博客编辑区默认情况下不为空，可以暂时不输入元素直接发表博客
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click() #找“发布文章”按钮
        #发布成功后会回到博客列表，再检查下标题即可
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > div.title")

#登录状态下--异常发布博客，未写标题、未写内容、标题内容均未写，都会出现弹窗
    def EditFailTestByLogin(self):
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        alert = self.driver.switch_to.alert  #获取窗口
        alert.accept()

#未登录状态下--可以正常编辑，但是点击发布博客后会强制回到登录页面，不论是否输入了标题或内容
    def EditTestNoLogin(self):
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container-login > div > h3")